//到货通知单
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var order_no=thisform.find('input[name=order_no]').val();
		var people_purchasing_no=thisform.find('input[name=people_purchasing_no]').val();
		var people_make_no=thisform.find('input[name=people_make_no]').val();
		var depot_code=thisform.find('input[name=depot_code]').val();
		$('#moveDepotDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#moveDepotDatagrid').datagrid({ 
			queryParams:{
				order_no:order_no,
				people_purchasing_no:people_purchasing_no,
				people_make_no:people_make_no,
				depot_code:depot_code
			}
		});
	})
	
	//更新行数据
	function moveDepotUpdateActions(value){
		$.post(moveDepotEditUrl,{id:value.id,section_no:value.section_no,people_make_no:value.people_make_no,people_purchasing_no:value.people_purchasing_no,contacts_company_no:value.contacts_company_no,depot_code:value.depot_code},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};

	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#moveDepotDatagrid').datagrid('validateRow', editIndex)){
			$('#moveDepotDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#moveDepotDatagrid').datagrid('selectRow', index);
				$('#moveDepotDatagrid').datagrid('endEdit', editIndex);
				$('#moveDepotDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#moveDepotDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};
	$('#moveDepotDatagrid').datagrid({
		//url:getmoveDepotUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getmoveDepotUrl,
		title:'调拨单列表',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'order_no',title:'单号',sortable:true},
		{field:'section_no',title:'申请部门',sortable:true,
				formatter:function(value,row,index){
					// htmlobj=$.ajax({type:'post',url:getSectionNameUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					// var info=eval('('+htmlobj.responseText+')');
					// return info.name;
					var json = section_lists;  //创建一个json
					var len = json.length;
					for (var i =0;i<len;i++){
						if(value==json[i].no){
							return json[i].name;
						}
					}
				},
				editor:{
					type:'combobox',
					options:{
						valueField:'no',
						textField:'name',
						method:'get',
						url:SectionlistUrl,
						required:true,
						validType:['selected[]']
					}
				}
		},
		{field:'people_make_no',title:'制单人',sortable:true,
				formatter:function(value,row,index){
					// htmlobj=$.ajax({type:'post',url:getPersonnelNameUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					// var info=eval('('+htmlobj.responseText+')');
					// return info.name;
					var json = personnel_lists;  //创建一个json
					var len = json.length;
					for (var i =0;i<len;i++){
						if(value==json[i].no){
							return json[i].name;
						}
					}
				},
				editor:{
					type:'combobox',
					options:{
						valueField:'no',
						textField:'name',
						method:'get',
						url:PersonnellistUrl,
						required:true,
						validType:['selected[]']
					}
				}
		},
		{field:'people_purchasing_no',title:'申请人',sortable:true,
				formatter:function(value,row,index){
					// htmlobj=$.ajax({type:'post',url:getPersonnelNameUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					// var info=eval('('+htmlobj.responseText+')');
					// return info.name;
					var json = personnel_lists;  //创建一个json
					var len = json.length;
					for (var i =0;i<len;i++){
						if(value==json[i].no){
							return json[i].name;
						}
					}
				},
				editor:{
					type:'combobox',
					options:{
						valueField:'no',
						textField:'name',
						method:'get',
						url:PersonnellistUrl,
						required:true,
						validType:['selected[]']
					}
				}
		},
		{field:'contacts_company_no',title:'往来单位',sortable:true,
				formatter:function(value,row,index){
					// htmlobj=$.ajax({type:'post',url:getContactsCompanyNameUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					// var info=eval('('+htmlobj.responseText+')');
					// return info.name;
					var json = contactscompany_lists;  //创建一个json
					var len = json.length;
					for (var i =0;i<len;i++){
						if(value==json[i].no){
							return json[i].name;
						}
					}
				},
				editor:{
					type:'combobox',
					options:{
						valueField:'no',
						textField:'name',
						method:'get',
						url:ContactsCompanylistUrl,
						required:true,
						validType:['selected[]']
					}
				}
		},
		{field:'depot_code',width:'200px',title:'目标仓库',sortable:true,
				formatter:function(value,row,index){
					htmlobj=$.ajax({type:'post',url:getDepotUrl,data:{code:value},async:false});//ajax动态获取出入库类别的名称
					var info=eval('('+htmlobj.responseText+')');
					return info.name;
				},
				editor:{
					type:'combobox',
					options:{
						valueField:'code',
						textField:'name',
						method:'get',
						url:depotListUrl,
						required:true,
						validType:['selected[]']
					}
				}
		},
		{field:'add_time',title:'添加时间',sortable:true},
		{field:'update_time',title:'更新时间',sortable:true},
		{field:'examine_time',title:'审核时间',sortable:true},
		{field:'status',title:'审核状态',sortable:true}
		]],
		queryParams:{
			erpId:erpId
		},
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
					//console.log(row);
					row.editing = false;
					moveDepotUpdateActions(row);
		},
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
	});
});
moveDepotObj={
	add:function(url){	//新增数据
		$('#moveDepotAddForm').form('submit',{
			url:url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	var result=eval('('+result+')');
                if(!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#moveDepotAddDialog').dialog('close');  
                    });
                }else{
					$.messager.alert('提示信息',result.message,'info',function(r){
                    	$('#moveDepotAddDialog').dialog('close');       
						$('#moveDepotDatagrid').datagrid('reload'); 
                    });
                }
            },
		});
	},
	save:function(url){//编辑后保存数据
		$('#moveDepotEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#moveDepotEditDialog').dialog('close');
                    });
                }else{
                    $('#moveDepotEditDialog').dialog('close');      
                    $('#moveDepotDatagrid').datagrid('reload');    
                }
            }
        });
	},
	saveBar:function(){//保存数据
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#moveDepotDatagrid').datagrid('validateRow', editIndex)){
			$('#moveDepotDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#moveDepotDatagrid').datagrid('acceptChanges');
		}
	},
	showAll:function(){//显示默认条件下所有数据
		$('#moveDepotDatagrid').datagrid({ queryParams:''});
	},
	infoBar:function(){//显示详情
		var moveDepotRow=$('#moveDepotDatagrid').datagrid('getSelections');
        if (moveDepotRow.length>1){
            $.messager.alert('提示','一次只能查看一条记录!','info');
        }else if(moveDepotRow.length==1){
			var id=moveDepotRow[0]['id'];
        	window.parent.showTab(moveDepotInfoUrl,'调拨单详情',id);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	reloadBar:function(){//刷新数据
		$('#moveDepotDatagrid').datagrid('reload');
	},
	recycle:function(){//查看回收站数据
		$('#moveDepotDatagrid').datagrid({ queryParams:{status:-1}});
	},
	addBar:function(){//显示添加框
		$('#moveDepotAddDialog').dialog('open').dialog('setTitle','添加');
		$('#moveDepotAddForm').form('clear');
		$('#moveDepot_section_no').combobox('select',0);
		$('#moveDepot_people_make_no').combobox('select',0);
		$('#moveDepot_people_purchasing_no').combobox('select',0);
		$('#moveDepot_contactsCompany').combobox('select',0);
		$('#moveDepot_depot_code').combobox('select',0);
		// $('#moveDepot_order_no').textbox('setValue',moveDepot_order_no);
		$.ajax({
            type:"POST",
            url:moveDepot_ordernoUrl,
            success: function(result){
                $('#moveDepot_order_no').textbox('setValue',result.order_no);
            }
        });
	},
	editBar:function(){//显示编辑框
		var moveDepotRow=$('#moveDepotDatagrid').datagrid('getSelections');
        if (moveDepotRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(moveDepotRow.length==1){            	
        	$('#moveDepotEditDialog').dialog('open').dialog('setTitle','编辑用户');
            $('#moveDepotEditForm').form('load',moveDepotRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	removeBar:function(){//删除数据
		var moveDepotRow=$('#moveDepotDatagrid').datagrid('getSelections');
        if (moveDepotRow.length>0){
        	$.messager.confirm('删除提示','真的要删除这'+moveDepotRow.length+'个信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<moveDepotRow.length;i++){
                		ids.push(moveDepotRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(moveDepotDelUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#moveDepotAddDialog').dialog('close');       
								$('#moveDepotDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	clearBar:function(){//删除/清空回收站数据
		var moveDepotRow=$('#moveDepotDatagrid').datagrid('getSelections');
        if (moveDepotRow.length>0){
        	$.messager.confirm('清空提示','真的要清空这'+moveDepotRow.length+'信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<moveDepotRow.length;i++){
                		ids.push(moveDepotRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(moveDepotClearUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#moveDepotAddDialog').dialog('close');       
								$('#moveDepotDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	undoBar:function(){//恢复回收站数据
		var moveDepotRow=$('#moveDepotDatagrid').datagrid('getSelections');
        if (moveDepotRow.length>0){
        	$.messager.confirm('恢复提示','真的要恢复这'+moveDepotRow.length+'信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<moveDepotRow.length;i++){
                		ids.push(moveDepotRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(moveDepotUndoUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#moveDepotAddDialog').dialog('close');       
								$('#moveDepotDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	examineBar:function(value){//审核数据
		var moveDepotRow=$('#moveDepotDatagrid').datagrid('getSelections');
		if(value==1){
			var text='确定审核通过吗,审核通过则冻结库存信息！';
		}
		if(value==2){
			var text='确定审核不通过吗！';
		}
        if (moveDepotRow.length>0){
        	$.messager.confirm('审核提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<moveDepotRow.length;i++){
                		ids.push(moveDepotRow[i].id);
						if(moveDepotRow[i].status=='审核通过'){
							$.messager.alert('错误提示','已审核通过不能再次审核！','error');
							return false;
						}
                	}
                	var id=ids.join(',');
                    $.post(moveDepotExamineUrl,{id:id,value:value},function(result){
                        if (result.status){
                            $('#moveDepotDatagrid').datagrid('reload');
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	pushOutBar:function(){
		var Row=$('#moveDepotDatagrid').datagrid('getSelections');
		var text='确定出库吗！';

        if (Row.length>0){
        	$.messager.confirm('提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<Row.length;i++){
						if(Row[i].status!='审核通过'){
							$.messager.alert('提示','未审核通过不能出库','error');
							return false;
						}
                		ids.push(Row[i].id);
						// if(Row[i].push_times > 0){
							// $.messager.confirm('推送提示','商品"'+Row[i].goods_name+'"已经推送，要重新推送吗');
						// }
                	}
					// alert(123);
                	var id=ids.join(',');
					$.post(moveDepotpushoutUrl,{id:id},function(result){
						if (result.status){
							$.messager.alert('出库成功',result.info);
						}else{
							$.messager.alert('提示',result.info,'error');
						}
					},'json').error(function(data){
						var info=eval('('+data.responseText+')');
						$.messager.confirm('提示',info.info,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
					});
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	pushInBar:function(){
		var Row=$('#moveDepotDatagrid').datagrid('getSelections');
		var text='确定推送吗！';

        if (Row.length>0){
        	$.messager.confirm('推送提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<Row.length;i++){
						if(Row[i].status!='审核通过'){
							$.messager.alert('推送提示','未审核通过不能推送','error');
							return false;
						}
                		ids.push(Row[i].id);
						// if(Row[i].push_times > 0){
							// $.messager.confirm('推送提示','商品"'+Row[i].goods_name+'"已经推送，要重新推送吗');
						// }
                	}
					// alert(123);
                	var id=ids.join(',');
					$.post(moveDepotpushinUrl,{id:id},function(result){
						if (result.status){
							$.messager.alert('推送成功',result.info);
						}else{
							$.messager.alert('错误提示',result.info,'error');
						}
					},'json').error(function(data){
						var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.info,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
					});
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}
}